/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestorxml;

import java.io.File;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
 * @author asc
 */
public class XMLWriter {

    
    private static DocumentBuilderFactory docFactory;
    private static DocumentBuilder docBuilder;
    private static Document doc;
    private static HashMap<String, Element> ArbolXml;

    public static void initialize() throws Exception
    {
        docFactory = DocumentBuilderFactory.newInstance();
        docBuilder = docFactory.newDocumentBuilder();
        ArbolXml = new HashMap<String, Element>();
    }
    
    public static void CreateRootElement(String Etiq)
    {
        // root element
        doc = docBuilder.newDocument();
        Element rootElement = doc.createElement(Etiq);
        doc.appendChild(rootElement);
        ArbolXml.put("root", rootElement);
    }
    
    public static void AppendChild(String Name, int node , String Parent)
    {
        // new elements
        Element e = doc.createElement(Name);
        ArbolXml.get(Parent).appendChild(e);
        ArbolXml.put(Name+String.valueOf(node), e);  
    }
    
    public static void AppendTextChild(String Name , String Parent)
    {
        // new elements
        ArbolXml.get(Parent).appendChild(doc.createTextNode(Name));
    }
    
    public static void SetAttribute(String Node,String Name, String Value)
    {
        // set attribute to staff element
        Attr attr = doc.createAttribute(Name);
        attr.setValue(Value);
        ArbolXml.get(Node).setAttributeNode(attr);
    }
    
    public static void AppendAtributteInfo(String node, String attr ,String info)
    {   
        String a = ArbolXml.get(node).getAttribute(attr);
        if(a.length() != 0) {
            a+=" ";
        }
        ArbolXml.get(node).setAttribute(attr, a+info);
    }
    
    public static void Finalize(String OutputFileName) throws Exception
    {
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "master.dtd");
        
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(OutputFileName));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");
    }
    
}